package com.powernode.advice;

import com.powernode.commons.Result;
import com.powernode.commons.ResultException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @AUTHOR X
 * @DATE 2021/10/28 21:00
 * @DESC AOP全局处理异常,这样controller中就不需要每个方法都try...catch了
 * 对外响应一个json数据,所以加 @ResponseBody 注解
 */
@Component
@ControllerAdvice
public class ExceptionHandle {

    @ResponseBody
    @ExceptionHandler
    public Result handler(Exception e){
        if (e instanceof ResultException){
            return Result.build((ResultException) e);
        } else {
            e.printStackTrace();
        }
        return null;
    }
}
